package com.nimo;

import com.alibaba.fastjson2.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;

/**
 * @author : chuf
 * @date : 2025-03-31
 **/
@Slf4j
public class AliPayTest {

    // 「沙箱环境」应用ID - 您的APPID，收款账号既是你的APPID对应支付宝账号。获取地址；https://open.alipay.com/develop/sandbox/app
    public static String app_id = "2021000147647930";
    // 「沙箱环境」商户私钥，你的PKCS8格式RSA2私钥 -【秘钥工具】所创建的公户私钥
    public static String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCldvKk9+o5/1gNRgALIeavXPsETkMdPc3CAKF6Vi4K6dijIOhzWebtCYGMF9yVrY4jffGuB8z9yVUNuI3AIxmSEjDv+jDZ1rojkS0twvJMPKaKwWzTrIbhdtLAi9m73fwSG70IOjLf0xxXAjmFKzqVgUgYYXlKwOVcSWYJV9Y+yXxUjkNT/TIOP3RjPYNUF0NIXULxrYDcZQtuj4FfO+0jz2+3qDYjLcEtd5U96+a0FmOhfo4vMbwhZ89f9ZDNe21ityQVGZ6wMpwrxUKl73E3kQw7+abgC5R6YwM6PY35cfUJ4t2czf/pMjiUFzZGsrHk2kPJMcN9NCfYyk5C7drtAgMBAAECggEBAJmbwI0n3llzVKIV7ypmkYsRTbNkmGfIHYFSJpa09M+K4mQmw8V6m6hIn+yXpl+KHJ+UZ05Bzat5Qi2XvHi33SA/JRFT+g/xiSJFv2RxS5E86Pt9r/MegqVDM0ovSrZe0YlaGPZPWTVy/idEWHPpetyahXyk2P5t7z/qoABie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境」支付宝公钥 -【秘钥填写】后提供给你的支付宝公钥
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt/+5IBLluAY4cdOqcPdPW/WKpAzSxbMeZ8Dvg8v3NwcARJsOhB86bHdOUFTAeoWLdWrc9g/oSHFUN8NqPNlAOAHtd92A1Oaqlu6FaOS/+NoVr8wGsZxtG+m05EE8gdAtEO7gmfTJh37UrA9u9NEzFXpwUzo4qbwZgvtNrvETQddDGPVkcHViaCSg2AxdHx0vFytn0sJT60L4S6Rw1IrtBCKwFCqBD4JyWGw2uDeRlx298vlrGdemNdfjpjGsUo/lz8yFfruoHe/7mtO2oXROJP+GLKplFlqUsBmJr4QXtmOb91HMOj1V8dAEuwtT1/QoNplAZxBTZj9szrdg4b4lOQIDAQAB";
    // 「沙箱环境」服务器异步通知回调地址
    public static String notify_url = "http://nimo.natapp1.cc/api/v1/alipay/alipay_notify_url";
    // 「沙箱环境」页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "https://gaga.plus";
    // 「沙箱环境」
    public static String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    // 签名方式
    public static String sign_type = "RSA2";
    // 字符编码格式
    public static String charset = "utf-8";

    private AlipayClient alipayClient;


    @Before
    public void init() {
        this.alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id,
                merchant_private_key,
                "json",
                charset,
                alipay_public_key,
                sign_type);
    }

    @Test
    public void test_aliPay_pageExecute() throws AlipayApiException {
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();  // 发送请求的 Request类
        request.setNotifyUrl(notify_url);
        request.setReturnUrl(return_url);

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "xfg2024092709120005");  // 我们自己生成的订单编号
        bizContent.put("total_amount", "0.01"); // 订单的总金额
        bizContent.put("subject", "测试商品");   // 支付的名称
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");  // 固定配置
        request.setBizContent(bizContent.toString());

        String form = alipayClient.pageExecute(request).getBody();
        log.info("测试结果：{}", form);
    }

    /**
     * 查询订单
     */
    @Test
    public void test_alipay_certificateExecute() throws AlipayApiException {

        AlipayTradeQueryModel bizModel = new AlipayTradeQueryModel();
        bizModel.setOutTradeNo("xfg2024092709120001");

        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizModel(bizModel);

        String body = alipayClient.execute(request).getBody();
        log.info("测试结果：{}", body);
    }

    /**
     * 退款接口
     */
    @Test
    public void test_alipay_refund() throws AlipayApiException {
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel refundModel = new AlipayTradeRefundModel();
        refundModel.setOutTradeNo("daniel82AAAA000032333361X03");
        refundModel.setRefundAmount("1.00");
        refundModel.setRefundReason("退款说明");
        request.setBizModel(refundModel);

        AlipayTradeRefundResponse execute = alipayClient.execute(request);
        log.info("测试结果：{}", execute.isSuccess());
    }

}
